package org.aldar.bquest.screens;

import android.os.Build;
import android.util.Log;
import android.view.Gravity;
import android.widget.FrameLayout;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;
import org.aldar.bquest.util.ExceptionUtil;
import org.andengine.opengl.view.RenderSurfaceView;
import org.andengine.ui.activity.SimpleBaseGameActivity;
import org.andengine.util.system.SystemUtils;

/**
 * User: Daria
 * Date: 9/12/14 3:44 PM
 */
public abstract class ScreenWithInterstitialBanner extends SimpleBaseGameActivity {

    //private AdView adView;
    private InterstitialAd adView;

    private boolean adFailedToLoad = false;


    public abstract boolean isBannerEnabled();

    /**
     * MY Class Comment
     */
    @Override
    protected void onSetContentView() {
        if (SystemUtils.isAndroidVersionOrLower(Build.VERSION_CODES.FROYO)) {
            super.onSetContentView();
            return;
        }
/*        super.onSetContentView();
    } // End of onSetContentView() //

    public void prepareBanner() {*/
        // CREATING the parent FrameLayout //
        final FrameLayout frameLayout = new FrameLayout(this);

        // CREATING the layout parameters, fill the screen //
        final FrameLayout.LayoutParams frameLayoutLayoutParams =
                new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
                        FrameLayout.LayoutParams.MATCH_PARENT, Gravity.BOTTOM);

        // CREATING a Smart Banner View //
// Create the adView.
        adView = new InterstitialAd(this);
        //adView.setAdSize(AdSize.SMART_BANNER);
        adView.setAdUnitId("ca-app-pub-2250022422565769/5773735130");
        adView.setAdListener(new AdListener() {
            @Override
            public void onAdClosed() {
                //showLevelMap();
            }
        });
        // Doing something I'm not 100% sure on, but guessing by the name //
        //adView.refreshDrawableState();
        //adView.setVisibility(AdView.VISIBLE);

        // ADVIEW layout, show at the bottom of the screen //
        final FrameLayout.LayoutParams adViewLayoutParams =
                new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,
                        FrameLayout.LayoutParams.WRAP_CONTENT,
                        Gravity.CENTER);

        // REQUEST an ad (Test ad) //
        AdRequest adRequest = new AdRequest.Builder()
                .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                .addTestDevice("014E05470A00500A")
                .addTestDevice("49FAD2D994A151F13559804BFCFFD423")
                .addTestDevice("79FCDC09C8784BEF268B2FF10F297E0B")
                .addTestDevice("ACA8F42811FDE2A6751F4DACB8362325")
                .addTestDevice("2298E755F623F3BD6EBED827EE37D54C")
                .build();
        // Start loading the ad in the background.
        adView.loadAd(adRequest);

        // RENDER the add on top of the scene //
        this.mRenderSurfaceView = new RenderSurfaceView(this);
        mRenderSurfaceView.setRenderer(mEngine, this);

        // SURFACE layout ? //
        final android.widget.FrameLayout.LayoutParams surfaceViewLayoutParams =
                new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
                        FrameLayout.LayoutParams.MATCH_PARENT, Gravity.CENTER);

        //new FrameLayout.LayoutParams(super.createSurfaceViewLayoutParams());

        // ADD the surface view and adView to the frame //
        frameLayout.addView(this.mRenderSurfaceView, surfaceViewLayoutParams);
        //frameLayout.addView(adView, adViewLayoutParams);

        // SHOW AD //
        this.setContentView(frameLayout, frameLayoutLayoutParams);
    }

    // Invoke displayInterstitial() when you are ready to display an interstitial.

    public void displayInterstitial(final Runnable actionAfterBannerClosed) {
        if (SystemUtils.isAndroidVersionOrLower(Build.VERSION_CODES.FROYO)) {
            actionAfterBannerClosed.run();
            return;
        }
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (isBannerEnabled()) {
                    if (adView.isLoaded()) {
                        adView.setAdListener(new AdListener() {
                            @Override
                            public void onAdClosed() {
                                actionAfterBannerClosed.run();
                            }

                            @Override
                            public void onAdFailedToLoad(int errorCode) {
                                Log.i("aldarlog", "Ad Failed To Load");
                                adFailedToLoad = true;
                                super.onAdFailedToLoad(errorCode);    //To change body of overridden methods use File | Settings | File Templates.
                            }

                            @Override
                            public void onAdOpened() {
                                Log.i("aldarlog", "Ad Opened");
                                super.onAdOpened();    //To change body of overridden methods use File | Settings | File Templates.
                            }

                            @Override
                            public void onAdLeftApplication() {
                                Log.i("aldarlog", "Ad Left Application");
                                actionAfterBannerClosed.run();
                            }
                        });
                        adView.show();
                    } else {
                        Log.i("aldarlog", "Since Ad Failed To Load - Proceeding to next screen...");
                        actionAfterBannerClosed.run();
                    }
                } else {
                    actionAfterBannerClosed.run();
                }
            }
        });

    }

    @Override
    public synchronized void onResumeGame() {
        //To change body of overridden methods use File | Settings | File Templates.
        try {
            super.onResumeGame();
        } catch (Throwable t) {
            ExceptionUtil.logThrowable(t, this, MainBoardScreen.getmSQLiteHelper());
        }
    }
}